program tenergy;
uses common;
uses tools,inp,ppot,decor,etools,relax;
Type
   r_xyz  = rsites3;
   r_e	  = rsites;
   r_tp	  = array[1..nsites] of integer;
Var
   i,j,k,l,m,n,o,dbg : integer;
   txt		     : text;
   r		     : r_xyz;
   ip		     : array[1..nsites] of atomrec;
   rb		     : array[1..nsites] of v33;
   itp,ito	     : r_tp;
   nsteps,omax,tinp  : integer;
   ueff,elas	     : myreal;
   uo,uo2	     : array[1..t_orb,1..atps] of myreal;
   tname,ss	     : mystring;
   rbt		     : v33;
   w1,w2,w3	     : v3;
   nofr		     : integer;
   dr_max,rcplus,cmp : myreal;
   po_max	     : i3;
   se		     : rsites;
   quiet,nat	     : integer;
   it,ntils	     : integer;
   b		     : i5;
   s1,s2,c1,c2	     : v3;
   dum		     : real;

begin
   export_occup:=false;
   for o:=1 to t_orb do o_rel[o]:=false;
   reset(txt,'INPUT/objects.def');
   readln(txt,deconame);
   readln(txt,i,o,j);
   if (j=1) then repre5:=true else repre5:=false;
   readln(txt,dpmodel);
   close(txt);
   dbg:=0;
   basis;
   readln(tnodes,multi,ntils);
   tnodes0:=tnodes div multi;
   if (tnodes>tdim) then begin
      writeln('Insufficient array dim for tiling (',tdim:1,'<',tnodes:1,')');
      halt;
   end;
   for i:=1 to 2 do
   begin
      for o:=1 to 5 do read(b[o]); readln;
      k:=nui(b); {should be =-nu_offs}
      bai[i]:=b;
      i5v2(bai[i],ba[i]);
      i5v2p(bai[i],bap[i]);
   end;
   UNIT_CELL(multi);  
   for i:=1 to 3 do begin
      for o:=1 to 3 do blt[o,i]:=bl3[i,o];
   end;
   PP_INP;
   i:=0;
   {two arbitrary points to lift e-degeneracy of non-identical structures}
   c1[1]:=0.25371; c1[2]:=-0.12472; c1[3]:=0.48901;
   c2[1]:=0.71554; c2[2]:=0.87145; c2[3]:=-0.22145;
   READ_DECO(i); {reads objects.def, ap.def}
   for it:=1 to ntils do begin
      IMPORT_ONEOF_TILINGS(tnodes,multi);
      TABLE_INIT;
      for i:=1 to tnodes do nan[i]:=0;
      for i:=1 to tnodes do NEW_NODE_OBJECTS(i,1);
      nat:=0; omax:=0;
      for o:=1 to 3 do begin s1[o]:=0; s2[o]:=0; end;
      for i:=1 to tnodes do begin
	 {writeln('node ',i:1,' ',nan[i]:1);}
	 for j:=1 to nan[i] do begin nat:=nat+1;
	    {for o:=1 to 3 do begin
	    r[nat,o]:=bound(n_r[i,j,o]+rt[i,o]);
	    dum:=r[nat,o]-c1[o];
	    s1[o]:=s1[o]+dum;
	    dum:=r[nat,o]-c2[o];
	    s2[o]:=s2[o]+dum;
	    end;}
	    itp[nat]:=n_a[i,j,1];
	    ito[nat]:=n_a[i,j,2];
	    ip[nat]:=n_para[i,j];
	    {basis for each atom relative to object origin [node]}
	    get_atom_basis(ip[nat],rb[nat]);
	    if (ito[nat]>omax) then omax:=ito[nat];
	    if (itp[nat]=0) then nat:=nat-1 else begin end;
	 end;
      end;
      rcplus:=1.0; {Angstrom}
      nntable(rcplus);
      cmp:=0;
      epot:=0;
      {for i:=1 to nat do begin
      writeln(i:1,' ',r[i,1]:1:4,' ',r[i,2]:1:4,' ',r[i,3]:1:4,' ',itp[i]:1);
      end;}
      UPPAV(epot,se);
      write(it:1,' ',epot/nat:1:8,' ');
      {for o:=1 to 3 do write(s1[o]:1:5);
      for o:=1 to 3 do write(s2[o]:1:5);}
      writeln;
   end;
end.
